var area_id = getUrlParam(decodeURIComponent(location.href), 'area_id') || 1;
var token = localStorage.wdczy_token;
var total_seats = 500;
var per_page = 30;
var total_pages = Math.ceil(total_seats / per_page);
var page = 1;
var selectSeats = [];
var maxSelect = 20;

/**
 * 获取上一页的页码
 * @param page
 * @returns {number}
 */
function getPagePre(page) {
    var pagePre = page - 1;
    return pagePre < 1 ? 0 : pagePre;
}

/**
 * 获取下一页的页码
 * @param page
 * @returns {number}
 */
function getPageAft(page) {
    var pageAft = page + 1;
    return pageAft > total_pages ? 0 : pageAft;
}

/**
 * 上一页
 */
function pageUp() {
    var pageNo = getPagePre(page);
    if (pageNo != 0) {
        AjaxGetSeatInfo(pageNo);
    }
}

/**
 * 下一页
 */
function pageDown() {
    var pageNo = getPageAft(page);
    if (pageNo != 0) {
        AjaxGetSeatInfo(pageNo);
    }
}

/**
 * 选择座位
 * @param mythis
 */
function chooseSeat(mythis) {
    if (selectSeats.length >= maxSelect) {
        layer.msg('最多选择' + maxSelect + '个座位');
    } else {
        var seat_id = $(mythis).data('id');
        if (selectSeats.indexOf(seat_id) == -1) {
            selectSeats.push(seat_id);
        }
        if (selectSeats.length > 0) {
            if ($('.btnBox02 span').length > 0) {
                $('.btnBox02 span').html(selectSeats.length);
            } else {
                $('.btnBox02 .btns').after('<span>' + selectSeats.length + '</span>');
            }
        } else {
            $('.btnBox02 span').remove();
        }
        $(mythis).attr('onclick', 'giveUpChooseSeat(this);');
        $(mythis).removeClass('noChoose').addClass('hasChose');
    }
}

/**
 * 放弃选择座位
 * @param mythis
 */
function giveUpChooseSeat(mythis) {
    var seat_id = $(mythis).data('id');
    if (selectSeats.indexOf(seat_id) != -1) {
        selectSeats.splice(selectSeats.indexOf(seat_id), 1);
    }
    if (selectSeats.length > 0) {
        if ($('.btnBox02 span').length > 0) {
            $('.btnBox02 span').html(selectSeats.length);
        } else {
            $('.btnBox02 .btns').after('<span>' + selectSeats.length + '</span>');
        }
    } else {
        $('.btnBox02 span').remove();
    }
    $(mythis).attr('onclick', 'chooseSeat(this);');
    $(mythis).removeClass('hasChose').addClass('noChoose');
}

/**
 * ajax请求获取座位数据
 * @constructor
 */
function AjaxGetSeatInfo(pageNo) {
    var post_url = toRoute('api/seat/getSeatsInfoDb');
    $.post(post_url, {'token': token, 'areaId': area_id, 'page': pageNo}, function (data) {
        if (data.errorCode == '200') {
            $('.aseat').empty();
            var j = 0;
            var html = '';
            for (var i = 0; i < data.data.length; i++) {
                if (area_id == 10) {
                    html += '<a class="sellout" href="javascript: void(0);"></a>';
                } else {
                    if (data.data[i].isOrder == 1) {
                        html += '<a class="sellout" href="javascript: void(0);"></a>';
                    } else {
                        if (selectSeats.indexOf(data.data[i].id) != -1) {
                            html += '<a class="hasChose" data-id="' + data.data[i].id + '" href="javascript: void(0);" onclick="giveUpChooseSeat(this);">' + data.data[i].seatName + '</a>';
                        } else {
                            html += '<a class="noChoose" data-id="' + data.data[i].id + '" href="javascript: void(0);" onclick="chooseSeat(this);">' + data.data[i].seatName + '</a>';
                        }
                    }
                }
                if ((i + 1) % 10 == 0) {
                    $('.aseat').eq(j).html(html);
                    html = '';
                    j++;
                }
            }
            if (!isNullOrEmpty(html) && j < 3) {
                $('.aseat').eq(j).html(html);
            }
            page = pageNo;
        }
    }, 'json');
}

$(function () {

    page = 1;
    AjaxGetSeatInfo(page);

});
